﻿@using MyData.Dto
@model AdminUserDto
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>信息管理系统</title>
    <script src="~/Scripts/jquery-3.3.1.min.js"></script>
    <link href="~/Content/default.css" rel="stylesheet" />
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
    <script src="~/Content/layer/layer.js"></script>
    <style>
        .treeview .list-group-item {
            cursor: pointer;
        }

        .treeview span.indent {
            margin-left: 10px;
            margin-right: 10px;
        }

        .treeview span.icon {
            width: 12px;
            margin-right: 5px;
        }

        .treeview .node-disabled {
            color: silver;
            cursor: not-allowed;
        }

        .node-treeview-expandible {
        }

            .node-treeview-expandible:not(.node-disabled):hover {
                background-color: #F5F5F5;
            }
    </style>
</head>
<body>
    <div class="tools">
        <ul class="toolbar">
            <li class="click" onclick="fnSave()"><span><img src="../Content/UI/images/t02.png" /></span>保存</li>
        </ul>
    </div>
    <div id="treeview-checkable" class=""></div>
    <input type="hidden" id="Id" value="@Model.Id.ToString()" />
</body>
<script src="~/Content/bootstrap-treeview.min.js"></script>
<script type="text/javascript">
    $(function () {
        var defaultData = [
         {
             text: '管理信息',
             tags: ['4'],
             state: {
                 checked: false
             },
             nodes: [
               {
                   text: '账号管理',
                   state: {
                       checked: false
                   },
               },
               {
                   text: '部门管理',
                   state: {
                       checked: false
                   },
               }, {
                   text: '设备管理',
                   state: {
                       checked: false
                   },
               }, {
                   text: '用户管理',
                   state: {
                       checked: false
                   },
               },
               {
                   text: '访客管理',
                   state: {
                       checked: false
                   },
               },
             ]
         },
         //{
         //    text: '会议信息',
         //    href: '#parent2',
         //    tags: ['0'],
         //    state: {
         //        checked: false
         //    },
         //    nodes: [
         //     {
         //         text: '会议管理',
         //         state: {
         //             checked: false
         //         },
         //     },
         //      {
         //          text: '人员管理',
         //          state: {
         //              checked: false
         //          },
         //      }
         //    ]
         //},
          {
              text: '考勤信息',
              state: {
                  checked: false
              },
              href: '#parent2',
              tags: ['0'],
              nodes: [
                   {
                       text: '员工记录',
                       state: {
                           checked: false
                       },
                   },
                   {
                       text: '访客记录',
                       state: {
                           checked: false
                       },
                   },
              
                {
                    text: '打卡记录',
                    state: {
                        checked: false
                    },
                },
                {
                    text: '图形展示',
                    state: {
                        checked: false
                    },
                },
                 {
                     text: '考勤记录',
                     state: {
                         checked: false
                     },
                 },
                  {
                      text: '车辆记录',
                      state: {
                          checked: false
                      },
                  },
                   {
                       text: '学校考勤',
                       state: {
                           checked: false
                       },
                   },
              ]
          },
            {
                text: '系统信息',
                href: '#parent2',
                state: {
                    checked: false
                },
                tags: ['0'],
                nodes: [
                 {
                     text: '系统设置',
                     state: {
                         checked: false
                     },
                 },
                  {
                      text: '同步日志',
                      state: {
                          checked: false
                      },
                  },
                 {
                     text: '假期管理',
                     state: {
                         checked: false
                     },
                 }]
            }
        ];
        //var defaultData = [
        // {
        //     text: '管理信息',
        //     tags: ['4'],
        //     state: {
        //         checked: false
        //     },
        //     nodes: [
        //       {
        //           text: '账号管理',
        //           state: {
        //               checked: false
        //           },
        //       }, {
        //           text: '设备管理',
        //           state: {
        //               checked: false
        //           },
        //       }
        //     ]
        // },
        // {
        //     text: '会议信息',
        //     href: '#parent2',
        //     tags: ['0'],
        //     state: {
        //         checked: false
        //     },
        //     nodes: [
        //      {
        //          text: '会议管理',
        //          state: {
        //              checked: false
        //          },
        //      }, {
        //          text: '人员管理',
        //          state: {
        //              checked: false
        //          },
        //      }
        //     ]
        // }
        //];

        var menus = '@Model.Menus';
        for (var i = 0; i < defaultData.length; i++) {
           // if (menus.indexOf(defaultData[i].text) != -1) {
             //   console.log(defaultData[i])
            var t = 0;
                for (var j = 0; j < defaultData[i].nodes.length; j++) {
                    if (menus.indexOf(defaultData[i].nodes[j].text) != -1) {
                        defaultData[i].nodes[j].state.checked = true;
                        t++;
                    }
                }
                if (defaultData[i].nodes.length == t + 1)
                {
                    defaultData[i].state.checked = true;
                }
                
            //}

        }

        var $checkableTree = $('#treeview-checkable').treeview({
            data: defaultData,
            showIcon: false,
            showCheckbox: true,
            onNodeChecked: function (event, node) { //选中节点
                var selectNodes = getChildNodeIdArr(node); //获取所有子节点
                if (selectNodes) { //子节点不为空，则选中所有子节点
                    $('#treeview-checkable').treeview('checkNode', [selectNodes, { silent: true }]);
                }
                var parentNode = $("#treeview-checkable").treeview("getNode", node.parentId);
                setParentNodeCheck(node);
            },
            onNodeUnchecked: function (event, node) { //取消选中节点
                // 取消父节点 子节点取消
                var selectNodes = setChildNodeUncheck(node); //获取未被选中的子节点
                var childNodes = getChildNodeIdArr(node);    //获取所有子节点
                if (selectNodes && selectNodes.length == 0) { //有子节点且未被选中的子节点数目为0，则取消选中所有子节点
                    console.log("反选");
                    $('#treeview-checkable').treeview('uncheckNode', [childNodes, { silent: true }]);
                }
                // 取消节点 父节点取消
                var parentNode = $("#treeview-checkable").treeview("getNode", node.parentId);  //获取父节点
                var selectNodes = getChildNodeIdArr(node);
                setParentNodeCheck(node);
            }
        });


    })
    // 选中父节点时，选中所有子节点
    function getChildNodeIdArr(node) {
        var ts = [];
        if (node.nodes) {
            for (x in node.nodes) {
                ts.push(node.nodes[x].nodeId);
                if (node.nodes[x].nodes) {
                    var getNodeDieDai = getChildNodeIdArr(node.nodes[x]);
                    for (j in getNodeDieDai) {
                        ts.push(getNodeDieDai[j]);
                    }
                }
            }
        } else {
            ts.push(node.nodeId);
        }
        return ts;
    }

    function setChildNodeUncheck(node) {
        if (node.nodes) {
            var ts = [];    //当前节点子集中未被选中的集合
            for (x in node.nodes) {
                if (!node.nodes[x].state.checked) {
                    ts.push(node.nodes[x].nodeId);
                }
                if (node.nodes[x].nodes) {
                    var getNodeDieDai = node.nodes[x];
                    console.log(getNodeDieDai);
                    for (j in getNodeDieDai) {
                        if (!getNodeDieDai.nodes[x].state.checked) {
                            ts.push(getNodeDieDai[j]);
                        }
                    }
                }
            }
        }
        return ts;
    }
    // 选中所有子节点时，选中父节点 取消子节点时取消父节点
    function setParentNodeCheck(node) {
        var parentNode = $("#treeview-checkable").treeview("getNode", node.parentId);
        if (parentNode.nodes) {
            var checkedCount = 0;
            for (x in parentNode.nodes) {
                if (parentNode.nodes[x].state.checked) {
                    checkedCount++;
                } else {
                    break;
                }
            }
            if (checkedCount == parentNode.nodes.length) {  //如果子节点全部被选 父全选
                $("#treeview-checkable").treeview("checkNode", parentNode.nodeId);
                setParentNodeCheck(parentNode);
            } else {   //如果子节点未全部被选 父未全选
                $('#treeview-checkable').treeview('uncheckNode', parentNode.nodeId);
                setParentNodeCheck(parentNode);
            }
        }
    }

    function fnSave() {
        var ts = "";
        var nodes = $('#treeview-checkable').treeview('getChecked', 0);
        for (var i = 0; i < nodes.length; i++) {
            ts = ts + nodes[i].text + ",";
        }
        $.post("../AdminUser/SaveMenus", { Id: $("#Id").val(), Menus: ts }, function (result) {
            if (result.state == 1) {
                layer.msg('操作成功');
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index)
                parent.$("#table").bootstrapTable('refresh');
            }
            else {
                layer.msg(result.message);
            }
        })

    }

</script>
</html>